package com.ywq.controller;


import com.ywq.service.StudentService;
import com.ywq.pojo.SSM_Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    //菜单跳转
    @RequestMapping(value = "/jump")
    public String jump(String target){
        String jmpUrl="/formjsp/"+target;
        return jmpUrl;
    }

    //添加学生信息
    @RequestMapping(value = "/addstudent")
    public ModelAndView add(SSM_Student student) {
        ModelAndView mv=new ModelAndView();
        String msg="";
        if(studentService.add(student)){
            msg="添加成功！";
        }else{
            msg="添加失败！";
        }
        mv.addObject("msg",msg);
        mv.setViewName("/formjsp/addstudent");
        return mv;
    }

    //删除学生信息
    @RequestMapping(value = "/delstudent")
    public ModelAndView del(Integer id) {
        ModelAndView mv=new ModelAndView();
        String msg="";
        if(studentService.deleteStudent(id)){
            msg="删除成功！";
        }else{
            msg="删除失败！";
        }
        mv.addObject("msg",msg);
        mv.setViewName("/formjsp/delstudent");
        return mv;
    }

    //修改：以id号查找学生信息
    @RequestMapping(value = "/upstudent")
    public ModelAndView upForId(Integer id) {
        ModelAndView mv=new ModelAndView();
        SSM_Student student=studentService.findStudentById(id);
        mv.addObject("student",student);
        mv.setViewName("/formjsp/upstudent");
        return mv;
    }

    @RequestMapping(value = "/upstudent2")
    public ModelAndView up2(SSM_Student stu) {
        ModelAndView mv=new ModelAndView();
        String msg="";
        if(studentService.updateStudent(stu)){
            msg="修改成功！";
        }else{
            msg="修改失败！";
        }
        mv.addObject("msg",msg);
        mv.setViewName("/formjsp/upstudent");
        return mv;
    }


    //查询：以id号查找学生信息
    @RequestMapping(value = "/selstudent")
    public ModelAndView sel(Integer id) {
        ModelAndView mv=new ModelAndView();
        SSM_Student student=studentService.findStudentById(id);
        mv.addObject("student",student);
        mv.setViewName("/formjsp/selstudent");
        return mv;
    }

    //以性别查找学生信息
    @RequestMapping(value = "/selstudentbysex")
    public ModelAndView selForSex(String sex) {
        ModelAndView mv=new ModelAndView();
        List<SSM_Student> studentList=studentService.findStudentBySex(sex);
        mv.addObject("studentList",studentList);
        mv.setViewName("/formjsp/selstudent");
        return mv;
    }

    /**
     * 查询所有用户
     */
    @RequestMapping("getAllStudent")
    public String getAllStudent(Model model){
        List<SSM_Student> stu = studentService.findAll();
        model.addAttribute("allStudentList",stu);
        return "/formjsp/selstudent";
    }

}
